import axios from "axios";
import { nanoid } from "nanoid";
export default {
  namespaced:true,
  actions: {
    addPersonWang(context,value){
       if(value.name.indexOf('王') === 0){
          context.commit('AddPerson',value)
       }else{
          alert('添加的姓名必须姓王!')
       }
    },
    addPersonServer(context){
       axios.get("https://api.uixsj.cn/hitokoto/get?type=social").then(
        res=>{
           context.commit('AddPerson',{id:nanoid(),name:res.data})
        },
        err=>{
           alert(err.message)
        }
       )
    }
  },
  mutations: {
    AddPerson(state, value) {
      // 复习unshift，添加至数组第一个数
      state.personList.unshift(value);
    },
  },
  state: {
    personList: [{ id: "001", name: "JaceZhou" }],
  },
  getters: {
     firstPersonName(state){
        return state.personList[0].name
     }
  },
};
